package com.wsc.personalblog.common;

import com.wsc.personalblog.exception.ArticleSaveException;
import com.wsc.personalblog.exception.CLikeFailedException;
import com.wsc.personalblog.exception.LoginFailException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.FileNotFoundException;

@ControllerAdvice //该注解把该类标识为异常处理器
@ResponseBody
@Slf4j
public class MyExceptionHandler {

    @ExceptionHandler(CLikeFailedException.class)
    public Result<String> cLikeFailedException(CLikeFailedException e){
        return Result.failed(e.getMessage());
    }

    @ExceptionHandler(FileNotFoundException.class)
    public Result<String> fileNotFoundException(FileNotFoundException e){
        return Result.failed(e.getMessage());
    }

    @ExceptionHandler(ArticleSaveException.class)
    public Result<String> articleSaveException(ArticleSaveException e){
        return Result.failed(e.getMessage());
    }

    @ExceptionHandler(LoginFailException.class)
    public Result<String> loginFailException(LoginFailException e){
        return Result.failed(e.getMessage());
    }

    // 兜底异常处理
    @ExceptionHandler(RuntimeException.class)
    public Result<String> RuntimeExceptionHandler(RuntimeException e){
        return Result.failed(e.getMessage());
    }
}
